package com.liang.wx.handler;

import com.liang.wx.enums.MessageTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MessageFactory implements InitializingBean {

    @Resource
    private List<MessageHandler> messageHandlers;

    private static final Map<MessageTypeEnum, MessageHandler> handlerMap = new HashMap<>();

    public static MessageHandler getMessageHandler(String type) {
        MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByType(type);
        return handlerMap.get(messageTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (MessageHandler messageHandler : messageHandlers) {
            handlerMap.put(messageHandler.getMessageType(), messageHandler);
        }
    }
}
